package com.liang.jiekou.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.entity.yifei.Purth;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * @author : ssy
 * @date: 2024-02-28
 * @Description:
 */
@Mapper
public interface PURTHMapper {
    @DS("demo")
    @Select("SELECT MD002 FROM INVMD WHERE MD001 = #{cd004} ")
    String getJJDW(String cd004);

    @DS("demo")
    @Select("SELECT COUNT(*)\n" +
            "FROM QMSTA\n" +
            "LEFT JOIN PURCD ON CD001 = TA001 AND CD002 = TA002 AND CD003 = TA003\n" +
            "LEFT JOIN PURCC ON CD001 = CC001 AND CC002 = CD002\n" +
            "LEFT JOIN PURMA ON MA001 = CC005 \n" +
            "WHERE TAC01 > 0 and (CD018-CD020) >0 \n" +
            "AND TA001=#{dhjydb} AND TA002 = #{dhjydh} AND TA003 = #{xh} AND CC005 = #{tg005}")
    Integer isExistToTheInvoice(String dhjydb, String dhjydh, String xh,String tg005);

    @DS("demo")
    @Select("SELECT TA001,TA002,TA003,CD004,CD005,CD006," +
            "TAC01,TA008,TA007,TA025,TAC02,TA026,CD013,CD007, " +
            "CD016,CD010,CD011,CD012,MA064,CD023,TAC03,TA041, " +
            "CD031,CD032,TA042,CD021,TA031,TAC04,TA028 " +
            "FROM QMSTA\n" +
            "LEFT JOIN PURCD ON CD001 = TA001 AND CD002 = TA002 AND CD003 = TA003\n" +
            "LEFT JOIN PURCC ON CD001 = CC001 AND CC002 = CD002\n" +
            "LEFT JOIN PURMA ON MA001 = CC005 \n" +
            "WHERE TAC01 > 0 and (CD018-CD020) >0 \n" +
            "AND TA001=#{dhjydb} AND TA002 = #{dhjydh} AND TA003 = #{xh} AND CC005 = #{tg005}")
    Map<String, Object> Select(String dhjydb, String dhjydh, String xh, String tg005);

    @DS("demo")
    @Select("SELECT " +
            "    CASE WHEN MB148 = COALESCE(MD002, MB148) THEN 1 ELSE 0 END AS is_same\n" +
            "FROM INVMB \n" +
            "LEFT JOIN INVMD ON MD001 = MB001 \n" +
            "WHERE " +
            "    MB001 = #{ph}")
    Boolean isSameUnit(String ph);

    @DS("demo")
    @Select("SELECT \n" +
            "    CAST(#{sum} * (MD003/MD004) AS DECIMAL(18, 2)) as sl\n" +
            "FROM \n" +
            "    INVMB \n" +
            "LEFT JOIN \n" +
            "    INVMD ON MD001 = MB001 \n" +
            "WHERE \n" +
            "    MB001 = #{cd004} ")
    Double calculate(Double sum,String cd004);

    @DS("demo")
    void insert(Purth purth);

    @DS("demo")
    @Select("Select COUNT(*)\n" +
            "from  PURTD\n" +
            "Left join PURTC on TC001=TD001 and TC002=TD002\n" +
            "Left join INVMB on TD004=MB001 \n" +
            "LEFT JOIN CMSMQ ON TC001=MQ001\n" +
            "Where TC004= #{tg005} \n" +
            "AND TD001 = #{cgdb} AND TD002 = #{cgdh} AND TD003 = #{xh} " +
            "AND TD008+TDC01-TD015-TDC02>0\n" +
            "AND TD016='N' and TD018='Y' \n" +
            "AND MQ003<>'27' AND TD033=0")
    Integer isPurchaseOrder(String cgdb, String cgdh, String xh, String tg005);

    @DS("demo")
    @Select("SELECT TD001, TD002, TD003, TD004, TD005, TD006, " +
            "TD007, TD008, TD009, TD010, TD011, TD012, TD013, " +
            "TD014, TD015, TD016, TD017, TD018, TD019, TD020, " +
            "TD021, TD022, TD023, TD024, TD025, TD026, TD027, " +
            "TD028, TD029, TD030, TD031, TD032, TD033, TD034, " +
            "TD035, TD036, TD037, TD038, TD039, TD040, TD041, " +
            "TD042, TD043, TD044, TD045, TD046, TD047, TD048, " +
            "TD049, TDC01, TDC02, TDC03, TDC04, TDC05, TDC06, " +
            "TD050, TD051 \n" +
            "from  PURTD\n" +
            "Left join PURTC on TC001=TD001 and TC002=TD002\n" +
            "Left join INVMB on TD004=MB001 \n" +
            "LEFT JOIN CMSMQ ON TC001=MQ001\n" +
            "Where TC004= #{tg005} \n" +
            "AND TC018 = #{sz} " +
            "AND TD001 = #{cgdb} AND TD002 = #{cgdh} AND TD003 = #{xh}\n" +
            "AND TD008+TDC01-TD015-TDC02>0\n" +
            "AND TD016='N' and TD018='Y' \n" +
            "AND MQ003<>'27' AND TD033=0")
    Map<String, Object> SelectByPur(String sz,String cgdb, String cgdh, String xh, String tg005);

    @DS("demo")
    @Delete("DELETE FROM PURTH WHERE TH001 = #{th001} AND TH002 = #{th002} AND TH003 = #{th003}")
    void deletePurth(Purth purth1);

    @DS("demo")
    @Delete("DELETE FROM PURTH WHERE TH001 = #{db} AND TH002 = #{dh}")
    void delete(String db, String dh);
}
